const perfix = "http://www.luhuaji.top:3000/api";

const http = {
    get(url, params) {
        // params  {a:3,b:4}  -> ?a=3&b=4
        if (params) {
            url += "?";
            let arr = [];
            for (const key in params) {
                arr.push(key + "=" + params[key]);
                // [a=3,b=4]
            }
            url += arr.join("&");
        }
        return new Promise((resolve) => {
            return fetch(perfix + url)
                .then((response) => response.json())
                .then((res) => {
                    if (res.status === 0) {
                        resolve(res);
                    } else {
                        alert(res.msg);
                    }
                });
        });
    },
    post(url, data) {
        return new Promise((resolve) => {
            return fetch(perfix + url, {
                    method: "POST", // or 'PUT'
                    body: JSON.stringify(data), // data can be `string` or {object}!
                    headers: new Headers({
                        "Content-Type": "application/json",
                    }),
                })
                .then((response) => response.json())
                .then((res) => {
                    if (res.status === 0) {
                        resolve(res);
                    }
                });
        });
    },
};


export default http;